package com.bot.edu.employmentsystem.resume.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bot.edu.employmentsystem.resume.entity.EmploymentResume;
import com.bot.edu.employmentsystem.resume.entity.EducationExperience;
import com.bot.edu.employmentsystem.resume.entity.PositonIntention;
import com.bot.edu.employmentsystem.resume.entity.ProjectExperience;
import com.bot.edu.employmentsystem.resume.entity.WorkExperience;
import com.bot.edu.employmentsystem.resume.mapper.EducationExperienceMapper;
import com.bot.edu.employmentsystem.resume.mapper.PositonIntentionMapper;
import com.bot.edu.employmentsystem.resume.mapper.ProjectExperienceMapper;
import com.bot.edu.employmentsystem.resume.mapper.WorkExperienceMapper;
import com.bot.edu.employmentsystem.resume.mapper.EmploymentResumeMapper;
import com.bot.edu.employmentsystem.resume.service.IEmploymentResumeService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 简历表
 * @Author: jeecg-boot
 * @Date:   2022-04-02
 * @Version: V1.0
 */
@Service
public class EmploymentResumeServiceImpl extends ServiceImpl<EmploymentResumeMapper, EmploymentResume> implements IEmploymentResumeService {

	@Autowired
	private EmploymentResumeMapper employmentResumeMapper;
	@Autowired
	private EducationExperienceMapper educationExperienceMapper;
	@Autowired
	private PositonIntentionMapper positonIntentionMapper;
	@Autowired
	private ProjectExperienceMapper projectExperienceMapper;
	@Autowired
	private WorkExperienceMapper workExperienceMapper;
	
	@Override
	@Transactional
	public void saveMain(EmploymentResume employmentResume, List<EducationExperience> educationExperienceList,List<PositonIntention> positonIntentionList,List<ProjectExperience> projectExperienceList,List<WorkExperience> workExperienceList) {
		employmentResumeMapper.insert(employmentResume);
		if(educationExperienceList!=null && educationExperienceList.size()>0) {
			for(EducationExperience entity:educationExperienceList) {
				//外键设置
				entity.setResumeId(employmentResume.getId());
				educationExperienceMapper.insert(entity);
			}
		}
		if(positonIntentionList!=null && positonIntentionList.size()>0) {
			for(PositonIntention entity:positonIntentionList) {
				//外键设置
				entity.setResumeId(employmentResume.getId());
				positonIntentionMapper.insert(entity);
			}
		}
		if(projectExperienceList!=null && projectExperienceList.size()>0) {
			for(ProjectExperience entity:projectExperienceList) {
				//外键设置
				entity.setResumeId(employmentResume.getId());
				projectExperienceMapper.insert(entity);
			}
		}
		if(workExperienceList!=null && workExperienceList.size()>0) {
			for(WorkExperience entity:workExperienceList) {
				//外键设置
				entity.setResumeId(employmentResume.getId());
				workExperienceMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(EmploymentResume employmentResume,List<EducationExperience> educationExperienceList,List<PositonIntention> positonIntentionList,List<ProjectExperience> projectExperienceList,List<WorkExperience> workExperienceList) {
		employmentResumeMapper.updateById(employmentResume);
		
		//1.先删除子表数据
		educationExperienceMapper.deleteByMainId(employmentResume.getId());
		positonIntentionMapper.deleteByMainId(employmentResume.getId());
		projectExperienceMapper.deleteByMainId(employmentResume.getId());
		workExperienceMapper.deleteByMainId(employmentResume.getId());
		
		//2.子表数据重新插入
		if(educationExperienceList!=null && educationExperienceList.size()>0) {
			for(EducationExperience entity:educationExperienceList) {
				//外键设置
				entity.setResumeId(employmentResume.getId());
				educationExperienceMapper.insert(entity);
			}
		}
		if(positonIntentionList!=null && positonIntentionList.size()>0) {
			for(PositonIntention entity:positonIntentionList) {
				//外键设置
				entity.setResumeId(employmentResume.getId());
				positonIntentionMapper.insert(entity);
			}
		}
		if(projectExperienceList!=null && projectExperienceList.size()>0) {
			for(ProjectExperience entity:projectExperienceList) {
				//外键设置
				entity.setResumeId(employmentResume.getId());
				projectExperienceMapper.insert(entity);
			}
		}
		if(workExperienceList!=null && workExperienceList.size()>0) {
			for(WorkExperience entity:workExperienceList) {
				//外键设置
				entity.setResumeId(employmentResume.getId());
				workExperienceMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		educationExperienceMapper.deleteByMainId(id);
		positonIntentionMapper.deleteByMainId(id);
		projectExperienceMapper.deleteByMainId(id);
		workExperienceMapper.deleteByMainId(id);
		employmentResumeMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			educationExperienceMapper.deleteByMainId(id.toString());
			positonIntentionMapper.deleteByMainId(id.toString());
			projectExperienceMapper.deleteByMainId(id.toString());
			workExperienceMapper.deleteByMainId(id.toString());
			employmentResumeMapper.deleteById(id);
		}
	}

	@Override
	public List<EmploymentResume> queryStuResumeList(String stuId) {
		return employmentResumeMapper.queryStuResumeList(stuId);
	}

}
